package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.Dish;
import com.woniuxy.entity.ModelItem;
import com.woniuxy.entity.Specs;
import com.woniuxy.entity.vo.SpecByDish;

import com.woniuxy.mapper.DishMapper;
import com.woniuxy.mapper.SpecsMapper;
import com.woniuxy.service.SpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecsServiceImpl extends ServiceImpl<SpecsMapper,Specs> implements SpecsService{
    @Autowired
    private SpecsMapper specsMapper;
    @Autowired
    private DishMapper dishMapper;

    @Transactional
    public void add(Specs specs){
        specs.setStatus("y");
        specsMapper.add(specs);
    }

    @Override
    public void update(Specs specs){
        specsMapper.update(specs);
    }

    @Override
    public List<Specs> getByCondition(Specs specs){
        return specsMapper.getByCondition(specs);
    }

    @Override
    public void delById(int id){
        specsMapper.deleteById(id);
    }

    @Override
    public Specs getByName(String name){
        return specsMapper.getByName(name);
    }

    @Override
    public List<Specs> getAll() {
        return specsMapper.getAll();
    }


    @Override
    public List<SpecByDish> getSpecsByDishId(Integer dishId) {
        //获取菜品
       
        Dish dish=dishMapper.getById(dishId);
        //根据菜品id获取规格id
        String spec_array = dish.getSpec_array();
        String[] specId = spec_array.split(",");

        //获取菜品规格的List
        List<Specs> specsList = new ArrayList<>();
        for (String s : specId) {
            if(!"".equals(s)){
                Specs specs = specsMapper.getById(Integer.parseInt(s));
                specsList.add(specs);
            }

        }


        List<SpecByDish> specByDishes = new ArrayList<>();


        //循环数据库获取单条规格
        for (Specs specs : specsList) {
            SpecByDish specByDish = new SpecByDish();
            specByDish.setSpecName(specs.getName());

            //将规格名，规格值存入Map键值对
            Map<String, String> bean = new HashMap<>();
            String specValue = specs.getValue();

            String sp = specValue.substring(1, specValue.length()-1);

            String[] speces = sp.split(",");
            for (String spece : speces) {
                String[] split = spece.split(":");
                bean.put(split[0].substring(1,split[0].length()-1),split[1].substring(1,split[1].length()-1));

            }
            //将规格名，规格值存入的Map键值对放入实体类
            specByDish.setSpecValue(bean);

            specByDishes.add(specByDish);
        }
        return specByDishes;


       /* List<ModelItem> modelItems = modelItemMapper.getAll();
        HashMap<String, String> beans = new HashMap<>();
        for (ModelItem modelItem : modelItems) {
            String specValue = modelItem.getSpecValue();

            String sp = specValue.substring(1, specValue.length()-1);

            String[] speces = sp.split(",");
            for (String spece : speces) {
                String[] split = spece.split(":");
                beans.put(split[0].substring(1,split[0].length()-1),split[1].substring(1,split[1].length()-1));


            }


        }

        return beans;*/


    }
}